package com.guigu.controller;

import com.guigu.dto.estimateDTO;
import com.guigu.entity.estimate;
import com.guigu.exception.SsmException;
import com.guigu.handler.MyHandlerExceptionResolver1;
import com.guigu.service.estimateService;
import com.guigu.vo.Result;
import com.guigu.vo.estimateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/estimate")
public class estimateController {

    @Autowired
    private estimateService esservice;


    @PostMapping("/queryestimate")
    public Result query(@RequestBody estimateDTO dto){
        try{
            List<estimateVO> list=this.esservice.query(dto);
            System.err.println(dto);
            if(!list.isEmpty()){
                return new Result<List<estimateVO>>("200", "true", "查询成功", list);
            }else if (dto.getName()!=null){
                return new Result<List<estimateVO>>("200", "true", "没有对应的数据", list);
            }
            else{
                return new Result<List<estimateVO>>("400", "false", "查询失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SsmException("异常");
        }
    }

    @PostMapping("/addes")
    public Result add(@RequestBody estimate es){
        try{
            Date currentDate = new Date();
            // 指定日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 格式化并打印日期
            String formattedDate = dateFormat.format(currentDate);
            System.out.println(es);
            es.setTimes(formattedDate);
            int id=this.esservice.add(es);
            if(id==1){
                return new Result<>("200", "true", "添加成功", null);
            }else{
                return new Result<>("400", "false", "添加失败", null);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/updatees")
    public Result update(@RequestBody estimate es){
        try{
          int id= this.esservice.update(es);
          if(id==1){
              return new Result<>("200", "true", "修改成功", null);
          }else{
              return new Result<>("400", "false", "修改失败", null);
          }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }

    @PostMapping("/deletees/{id}")
    public Result delete(@PathVariable int id){
        try{
            int  deleted = this.esservice.delete(id);
            if (deleted==1) {
                return new Result<>("200", "true", "删除成功", null);
            } else {
                return new Result<>("400", "false", "删除失败", null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new SsmException("异常");
        }
    }
}
